<template>
  <div>
    <div class="block">
      <div class="condition">
        <div class="text1">手动盘点</div>
      </div>
      <div class="bg-card">
        <el-form
          :inline="true"
          :model="topForm"
          ref="topForm"
          :rules="topFormRule"
        >
          <el-row>
            <el-form-item prop="value2" label="仓库">
              <el-input :value="$route.query.warehouse === 1 ? '防汛仓库' : '备品备件仓库'" disabled></el-input>
            </el-form-item>
            <el-form-item label="盘点申请时间">
              <el-date-picker
                v-model="checkFillTime"
                type="date"
                format="yyyyMMdd"
                value-format="yyyyMMdd"
                :picker-options="pickerOptions"
                placeholder="选择日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item prop="checkStatus" label="货品状态">
              <el-select v-model="topForm.checkStatus" :value="1">
                <el-option
                  v-for="item in checkTypeOpt"
                  :key="item.index"
                  :label="item.value"
                  :value="item.index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item prop="goodsId01" label="货品编号">
              <el-input
                v-model="topForm.goodsId01"
                style="width:100px"
              ></el-input>
            </el-form-item>
            <el-form-item prop="goodsName" label="货品名称">
              <el-input
                v-model="topForm.goodsName"
                style="width:100px"
              ></el-input>
            </el-form-item>
            <el-form-item prop="specification" label="型号规格">
              <el-input v-model="topForm.specification"></el-input>
            </el-form-item>
            <el-form-item prop="goodsUnit" label="单位">
              <el-input
                v-model="topForm.goodsUnit"
                style="width:80px"
              ></el-input>
            </el-form-item>
            <el-form-item prop="goodsNum" label="库存数量">
              <el-input v-model="topForm.goodsNum" style="width:80px" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item prop="checkNumber" label="盘点数量">
              <el-input
                v-model="topForm.checkNumber"
                style="width:80px"
              ></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item prop="checkRemakes" label="备注">
              <el-input
                v-model="topForm.checkRemakes"
                type="textarea"
                style="width:200px;;margin-top:30px"
              ></el-input>
            </el-form-item>
          </el-row>
          <el-row>
            <div class="bg-card">
              <el-button
                type="success"
                @click="handleSubmit(0)"
                style="margin-left:30px"
                >保存盘点单</el-button
              >
              <el-button
                type="primary"
                @click="handleSubmit(1)"
                style="margin-left:30px"
                >提交</el-button
              >
              <el-button
                type="info"
                @click="handleReturn"
                style="margin-left:30px"
                >返回</el-button
              >
            </div>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      topForm: {
      },
      checkFillTime: "",
      checkTypeOpt: [{ index: 1, value: "正常" }, { index: 2, value: "损坏" }],
      topFormRule: {
        goodsId01: [{ required: true, message: "必填项", trigger: "blur" }],
        goodsName: [{ required: true, message: "必填项", trigger: "blur" }],
        specification: [{ required: true, message: "必填项", trigger: "blur" }],
        goodsUnit: [{ required: true, message: "必填项", trigger: "blur" }],
        checkNumber: [
          { required: true, message: "必填项", trigger: "change" },
        ],
        goodsNum: [{ required: true, message: "必填项", trigger: "change" }],
      },
      query: {
        currentPage: 0,
        num: "",
        pageSize: 0
      },
      tableData: [],
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            }
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            }
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            }
          }
        ]
      }
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    async handleSubmit(status) {
      this.$refs.topForm.validate(async (valid) => {
        if (valid) {
          if (!this.checkFillTime) {
            return this.$message.info("请填写盘点时间");
          }
          this.topForm.checkNumber = parseInt(this.topForm.checkNumber)
          this.topForm.checkFillTime = this.checkFillTime
          var arr = [];
          arr[0] = this.topForm
          const res = await this.$http.post(
            `/restful/v1/dd/check/checkGoodsFilling?status=${status}&checkFillTime=${this.checkFillTime}&checkNum=${this.checkNum}`,
            arr
          );
          if (res.data.code === 1) {
            this.$message.success("操作成功");
            this.$router.push({
              path: `/home/storehouse${this.$route.query.warehouse}/inventory`
            });
          } else {
            this.$message.error(res.data.message);
          }
        }
      });
    },
    handleReturn() {
      this.$router.go(-1);
    },
    async fetch() {
      const res = await this.$http.post(
        `/restful/v1/dd/goods/getGoodsInfo?goodsId=${this.$route.query.id}&wareHouse=${this.$route.query.warehouse}`
      );
      // const res2 = await this.$http.get("/restful/v1/dd/permit/getUserDetail");
      this.topForm = {
        goodsId01: res.data.object.goodsId01,
        goodsName: res.data.object.goodsName,
        specification: res.data.object.specification,
        goodsUnit: res.data.object.goodsUnit,
        goodsNum: Number(res.data.object.goodsNum),
        id: 0,
        warehouse: this.$route.query.warehouse,
        checkStatus: 1
        // scarpPeople: res2.data.object.userName
      }
    },
  }
};
</script>
